import wepy from 'wepy'
import Dialog from '../../components/vant/dialog/dialog'

export default class extends wepy.mixin {
  data = {
    cart: []
  }

  methods = {
    setpperChange (id,e) {
      const count = e.detail
      this.$parent.editCount(id, count)
    },
    // 点击复选框
    checkboxChange (id) {
      this.$parent.updataIsCheck(id)
    },
    // 点击删除
    removeHandle (id) {
      Dialog.confirm({
        message: '确定删除吗？'
      }).then(() => {
        this.$parent.removeGoodsItem(id)
        this.$apply()
      }).catch(() => {
        wepy.baseToast('已取消删除')
      })
      
    },
    // 点击全选
    fullChange (e) {
      console.log(e.detail)
      this.$parent.uadateAllIscheck(e.detail)
    },
    // 
    goGoodsDetail (id) {
      wepy.navigateTo({
        url: '/pages/goods_detail/main?goods_id=' + id
      })
    },
    // 提交订单
    submitOrder () {
      const falg = this.cart.every(x => !x.isCheck)
      if (falg) return wepy.baseToast('商品为空')
      wepy.navigateTo({
        url: '/pages/order/index'
      })
    }
  }
  onLoad (options) {
    this.cart = this.$parent.globalData.cart
  }
  computed = {
    // 是否显示空购物车
    isEmptyCart () {
      return this.cart.length === 0 ? true : false 
    },
    // 计算要购买的商品
    totaPrice () {
      let total = 0
      this.cart.forEach(item => {
        if (item.isCheck) {
          total += item.price * item.count
        }
      })
      return total * 100
    },
    // 是否全选
    isSelectAll () {
      return this.cart.every(item => item.isCheck)
    }
  }

  onShow () {
    this.$parent.setTabbatBadge()
  }
}
